package com.truckdispatching.http.util;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.truckdispatching.http.base.HttpController;

public abstract class BaseHttpController extends HttpController {

	public abstract void onSuccess(String response);

	public abstract void onError(int errorCode, String errorMessage);

	@Override
	public void onRequestSuccess(String content) {
		onSuccess(content);
	}

	@Override
	public void onRequestError(int errorCode, String errorMessage) {
		onError(errorCode, errorMessage);
	}

	private OnResponseListener mOnResponseListener;

	public void setOnResponseListener(OnResponseListener listener) {
		this.mOnResponseListener = listener;
	}

	protected void callOnResponseListener(Object response) {
		if (mOnResponseListener != null) {
			if (mOnResponseListener instanceof OnResponseHandler) {
				((OnResponseHandler) mOnResponseListener).obtainMessage(OnResponseHandler.SUCESS, response).sendToTarget();
			} else {
				mOnResponseListener.onResponse(response);
			}
		}
	}

	protected void callOnResponseErrorListener(int errorCode, String errorMessage) {
		if (mOnResponseListener != null) {
			if (mOnResponseListener instanceof OnResponseHandler) {
				((OnResponseHandler) mOnResponseListener).obtainMessage(OnResponseHandler.ERROR, errorCode, 0, errorMessage).sendToTarget();
			} else {
				mOnResponseListener.onResponseError(errorCode, errorMessage);
			}
		}
	}

	public JSONObject createJOSNObject(String json) {
		try {
			if (json != null && !"".equals(json)) {
				return new JSONObject(json);
			}
		} catch (JSONException e) {
		}
		return null;
	}

	public JSONObject getJSONObjectFromJSON(JSONObject json, String key, JSONObject defaultValue) {
		try {
			if (json != null && json.has(key)) {
				return json.getJSONObject(key);
			}
		} catch (JSONException e) {
		}
		return defaultValue;
	}

	public JSONObject getJSONObjectFromJSONArray(JSONArray jsonArray, int index, JSONObject defaultValue) {
		try {
			if (index < jsonArray.length()) {
				return jsonArray.getJSONObject(index);
			}
		} catch (JSONException e) {
		}
		return defaultValue;
	}

	public JSONArray getJSONArrayFromJSON(JSONObject json, String key, JSONArray defaultValue) {
		try {
			if (json != null && json.has(key)) {
				return json.getJSONArray(key);
			}
		} catch (JSONException e) {
		}
		return defaultValue;
	}

	public String getStringFromJSON(JSONObject json, String key, String defaultValue) {
		try {
			String iTemp;
			if (json != null && json.has(key)) {
				iTemp = json.getString(key);
				if (iTemp == null) {
					return defaultValue;
				} else {
					if (iTemp.equals("null")) {
						return defaultValue;
					} else {
						return iTemp;
					}
				}
			}
		} catch (JSONException e) {
		}
		return defaultValue;
	}

	public String getStringFromJSONArray(JSONArray jsonArray, int index, String defaultValue) {
		try {
			if (index < jsonArray.length()) {
				return jsonArray.getString(index);
			}
		} catch (JSONException e) {
		}
		return defaultValue;
	}

	public boolean getBooleanFromJSON(JSONObject json, String key, boolean defaultValue) {
		try {
			boolean iTemp;
			if (json != null && json.has(key)) {
				iTemp = json.getBoolean(key);
				return iTemp;
			}
		} catch (JSONException e) {
		}
		return defaultValue;
	}

	public int getIntFromJSON(JSONObject json, String key, int defaultValue) {
		try {
			if (json != null && json.has(key)) {
				return json.getInt(key);
			}
		} catch (JSONException e) {
		}
		return defaultValue;
	}

	public long getLongFromJSON(JSONObject json, String key, Long defaultValue) {
		try {
			if (json != null && json.has(key)) {
				return json.getLong(key);
			}
		} catch (JSONException e) {
		}
		return defaultValue;
	}
}
